package org.ouwa.lms.client.ui;

import org.ouwa.lms.client.manager.ManagerRegistry;
import org.ouwa.lms.client.support.event.ApplicationEvent;
import org.ouwa.lms.client.support.event.ApplicationEventListener;
import org.ouwa.lms.client.support.event.ApplicationEventListenerCollection;
import org.ouwa.lms.client.support.event.ApplicationEventSource;

import com.google.gwt.user.client.ui.Composite;

/**
 * Serves as a base class for all panes in the application.
 *
 * @author lviet
 */

public abstract class EventPane extends Composite implements ApplicationEventSource {
	private final ApplicationEventListenerCollection listeners;
	private final ManagerRegistry managerRegistry;
	
	/**
     * Constructs a new <code>Pane</code>.
     */
	protected EventPane(ManagerRegistry managerRegistry) {
		this.managerRegistry = managerRegistry;
		listeners = new ApplicationEventListenerCollection();
	}
	
	/**
     * {@inheritDoc}
     */
	@Override
	public void addListener(ApplicationEventListener listener) {
		listeners.add(listener);
	}
	
	/**
     * {@inheritDoc}
     */
	@Override
	public void clearListeners() {
		listeners.clear();
	}
	
	/**
     * {@inheritDoc}
     */
	@Override
	public void removeListener(ApplicationEventListener listener) {
		listeners.remove(listener);
	}
	
	/**
     * Fires the given event to all the registered listener.
     *
     * @param event The event to fire.
     */
    protected void fireEvent(ApplicationEvent event) {
        listeners.fireEvent(event);
    }
    
    /**
     * Returns the manager registry associated with this pane.
     *
     * @return The manager registry associated with this pane.
     */
    protected ManagerRegistry getManagerRegistry() {
        return managerRegistry;
    }
}
